package com.baba.bracelet.controller;

import com.alibaba.fastjson.JSONObject;
import com.baba.bracelet.base.BaseApiService;
import com.baba.bracelet.base.BaseResponse;
import com.baba.bracelet.entity.PrisonEntity;
import com.baba.bracelet.service.PrisonService;
import com.baba.bracelet.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wulongbo
 * @version 1.0
 * @date 2020/6/13 17:42
 * @description
 */
@RestController
@RequestMapping("/excel")
@CrossOrigin
@Api(tags = "Excel导入导出模块")
public class ExcelController extends BaseApiService<JSONObject> {

    @Autowired
    private PrisonService prisonService;

    @ApiOperation(value="Excel导出", notes="Excel导出")
    @RequestMapping("exportExcel")
    public BaseResponse<JSONObject> export(HttpServletResponse response){
        List<PrisonEntity> prisonList = prisonService.list();
        //导出操作
        ExcelUtil.exportExcel(prisonList,"犯人信息","sheet1",PrisonEntity.class,"监狱犯人信息表.xls",response);
        return setResultSuccess("导出成功！");
    }

    @ApiOperation(value="Excel导入", notes="Excel导入")
    @RequestMapping("importExcel")
    public BaseResponse<JSONObject> importExcel() {
        String filePath = "C:\\testInfo.xls";
        //解析excel，
        List<PrisonEntity> prisonEntityList = ExcelUtil.importExcel(filePath, 1, 1, PrisonEntity.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【" + prisonEntityList.size() + "】行");

        prisonService.saveBatch(prisonEntityList);
        List<PrisonEntity> userList2 = prisonService.list();
        return setResultSuccess("导入成功！，请到C盘查看 testInfo.xls");
    }
}
